package com.mirror.llk.controller;

import java.util.Map;

import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mirror.frame.controller.BaseController;
import com.mirror.frame.log4j.ILog;
import com.mirror.frame.log4j.Logger;
import com.mirror.frame.utils.BaseUtils;
import com.mirror.llk.bean.request.ThemeListRequest;
import com.mirror.llk.bean.request.ThemePublishRequest;
import com.mirror.llk.bean.response.ThemeListResponse;
import com.mirror.llk.bean.response.ThemePublishResponse;
import com.mirror.llk.service.ThemeService;
import com.mirror.llk.util.Constant;
import com.mirror.llk.util.XStreamUtils;

@Controller
@RequestMapping("/theme")
public class ThemeController extends BaseController {

    @Logger
    private static ILog log;

    @Autowired
    private ThemeService themeService;

    /**
     * 发布主题
     */
    @RequestMapping(value = "/publish", method = {RequestMethod.POST})
    public String publish(@RequestBody String inXml, Map<String, String> model) throws Exception {
        // 日志记录
        MDC.put("logId", BaseUtils.getLogId());
        log.info("进入主题发布接口");

        // 解析请求报文
        ThemePublishRequest requestBean = (ThemePublishRequest) XStreamUtils.parseXmlToObj(inXml,
                ThemePublishRequest.class);

        // 业务层处理
        ThemePublishResponse response = themeService.publishTheme(requestBean);

        // 返回响应报文
        model.put(Constant.RETURN_MESSAGE, XStreamUtils.parseObjToXml(response));

        log.info("主题发布完成");
        return RET_JSP;
    }

    /**
     * 主题列表
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    public String list(@RequestBody String inXml, Map<String, String> model) throws Exception {
        // 日志记录
        MDC.put("logId", BaseUtils.getLogId());
        log.info("进入主题列表接口");

        // 解析请求报文
        ThemeListRequest requestBean = (ThemeListRequest) XStreamUtils.parseXmlToObj(inXml,
                ThemeListRequest.class);

        // 业务层处理
        ThemeListResponse response = themeService.listTheme(requestBean);

        // 返回响应报文
        model.put(Constant.RETURN_MESSAGE, XStreamUtils.parseObjToXml(response));

        log.info("主题列表完成");
        return RET_JSP;
    }
}